package org.opsli.modulars.test;


import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.Notification;

public class Jiguang {
    private String MASTER_SECRET = "507429e5d42dae89cfa998fd";
    private String APP_KEY = "1b71a4392bf273fbc2d37f7a";
    private ClientConfig config = ClientConfig.getInstance();
    private JPushClient client = new JPushClient(MASTER_SECRET, APP_KEY, null, config);

    /**
     * 面向平台：安卓端
     * 推送目标：单推个人（别名推送）
     */
    public PushResult pushAline(String ID, String alert, String title) {
        PushResult result = null;
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//推送平台
                .setAudience(Audience.alias(ID))//推送目标
                .setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder().
                        setAlert(alert).setTitle(title).build()).build())//通知消息（标题，内容）
                .setOptions(Options.newBuilder().setApnsProduction(true).setTimeToLive(86400).build()//离线消息保存时间
                )
                .build();
        try {
            result = client.sendPush(payload);//推送
            System.out.println("推送结果：" + result.getResponseCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 面向平台：安卓端
     * 推送目标：所有用户
     */
    public PushResult pushAll() {
        PushResult result = null;
        PushPayload pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//推送平台
                .setAudience(Audience.all())//推送目标
                .setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder().
                        setAlert("内容").setTitle("标题").build()).build())//通知消息（标题，内容）
                .setOptions(Options.newBuilder().setApnsProduction(true).setTimeToLive(86400).build()//离线消息保存时间
                )
                .build();
        try {
            result = client.sendPush(pushPayload);//推送
            System.out.println("推送结果：" + result.getResponseCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        Jiguang jiguang = new Jiguang();
        //每个app项目运行时前端会触发用户接口获取id（别名）并向极光推送接口别名设置赋值
        //所以根本不需要考虑前端的别名设置是什么
        //只需获取到推送用户的id也就是和前端设置所对应的id（别名）
        jiguang.pushAline("1455020265405804545", "嘿嘿嘿嘿嘿5", "哈哈哈哈哈");

        //jiguang.pushAline("AAAC","设备内容22","设备标题22");
        //jiguang.pushAll();
    }
}

